﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using System.Diagnostics;

namespace XNADota.ContentPipeline.Terrain
{
    [ContentSerializerRuntimeType("XNADota.Terrain.Unit, XNADota")]
    public class UnitContent
    {
        public UnitContent(WCLoader Loader)
        {
            TypeId = Loader.ReadString(4);

            Variation = Loader.ReadInt32();
            Position = Loader.ReaderVector3();
            Rotation = new Vector3(0, Loader.ReadFloat(), 0);
            Scale = Loader.ReaderVector3();
            Flags = Loader.ReadByte();
            PlayerNumber = Loader.ReadInt32();

            // Read two null bytes
            Loader.ReadByte();
            Loader.ReadByte();

            HitPoints = Loader.ReadInt32();
            ManaPoints = Loader.ReadInt32();

            // Dropped items after death, should be zero
            int DropItemCount = Loader.ReadInt32();
            Debug.Assert(DropItemCount == -1);

            GoldAmount = Loader.ReadInt32();
            TargetAcquisition = Loader.ReadInt32();
            HeroLevel = Loader.ReadInt32();

            // Inventory count, should be zero
            int InventoryCount = Loader.ReadInt32();
            Debug.Assert(InventoryCount == 0);

            // Modified abilities, should be zero?
            int ModifiedAbilities = Loader.ReadInt32();
            Debug.Assert(ModifiedAbilities == 0);
             
            // Unknown flag, just leave it here
            RndFlag = Loader.ReadInt32();
            Debug.Assert(RndFlag == 0);
            int k = Loader.ReadInt32();
            k = Loader.ReadInt32();
            k = Loader.ReadInt32();
            k = Loader.ReadInt32();
            k = Loader.ReadInt32();
              
            CustomColor = Loader.ReadInt32();
            WayGate = Loader.ReadInt32();
            CreationNumber = Loader.ReadInt32();
        }

        public string TypeId;
        public int Variation;
        public Vector3 Position;
        public Vector3 Rotation;
        public Vector3 Scale;
        public int Flags;
        public int PlayerNumber;
        public int HitPoints;
        public int ManaPoints;
        public int GoldAmount;
        public int TargetAcquisition;
        public int HeroLevel;
        public int StrengthOfHero;
        public int AgilityOfHero;
        public int IntelligenceOfHero;
        public int RndFlag;
        public int CustomColor;
        public int WayGate;
        public int CreationNumber;

    }

    [ContentSerializerRuntimeType("XNADota.Terrain.WarCraftUnitsDoo, XNADota")]
    public class WarCraftUnitsDooContent
    {
        public WarCraftUnitsDooContent(WCLoader Loader)
        {
            Loader.ExpectTag("W3do");
            Version = Loader.ReadInt32();
            SubVersion = Loader.ReadInt32();
            Debug.Assert(Version == 8);
            Debug.Assert(SubVersion == 11);

            UnitCount = Loader.ReadInt32();
            Units = new UnitContent[UnitCount];
            for (int i = 0; i < UnitCount; i++)
                Units[i] = new UnitContent(Loader);
        }

        public int Version;
        public int SubVersion;
        public int UnitCount;
        public UnitContent[] Units;
    }
}
